package com.example.myapplication01;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.myapplication01.adapter.MyAdapter;
import com.example.myapplication01.anim.MyItemAnimator;
import com.example.myapplication01.base.BaseActivity;
import com.example.myapplication01.inter.GitHubService;
import com.example.myapplication01.inter.Service;
import com.example.myapplication01.mvp.presenter.MainPresenter;
import com.example.myapplication01.mvp.view.MainView;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;

//import retrofit2.Call;
//import retrofit2.Retrofit;
//import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @user xunua
 * @email  xunua@outlook.com
 * @creat time 2020/4/10 15:56
 *
 * RecyclearView的原生使用   MyAdapter  和 动画的实现  瀑布流的实现
 *
 * 切记不可使用notifyDataSetChanged() 会导致动画失效
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainView,MyAdapter.OnChildClickListener {
    RecyclerView recyclerView;
    MyAdapter adapter;
    ArrayList arrayList;
    TextView textView;


    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        recyclerView=findViewById(R.id.recyclerView);
        textView=findViewById(R.id.tv01);
        mPresenter=new MainPresenter(this);
    }

    @Override
    public void initData() {
        arrayList=new ArrayList();
        for (int i = 0; i < 100; i++) {
            double random = Math.random()*100;
            if (random < 20) {
                arrayList.add("这是item");
            }else if (random>20&&random<60){
                arrayList.add("这是item12333333333333");
            }else {
                arrayList.add("这是第撒肯定撒开发撒【psfa哥");
            }
        }
        adapter = new MyAdapter(this, arrayList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4,RecyclerView.VERTICAL,false);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {//跨列或行
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 4;
                }
                return 1;
            }
        });
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);//实现瀑布流++-
        recyclerView.setLayoutManager(linearLayoutManager);//设置布局
        MyItemAnimator defaultItemAnimator = new MyItemAnimator();
        defaultItemAnimator.setSupportsChangeAnimations(true);//默认不支持改变动画
//        defaultItemAnimator.setMoveDuration(1000);//移动动画
//        defaultItemAnimator.setRemoveDuration(500);//设置动画的时间
        defaultItemAnimator.setChangeDuration(1000);
        recyclerView.setItemAnimator(defaultItemAnimator);//设置动画系统
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {//item其余部分背景颜色  被item覆盖
                super.onDraw(c, parent, state);
                c.drawColor(Color.GRAY);
            }

            @Override
            public void onDrawOver(@NonNull final Canvas c, @NonNull final RecyclerView parent, @NonNull RecyclerView.State state) {//前景色 覆盖item
                super.onDrawOver(c, parent, state);
//                c.drawColor(Color.RED);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.asfdasf);
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();
                c.drawBitmap(bitmap,(parent.getWidth()/2)-(width/2),((parent.getHeight()-height)/2),null);
                bitmap.recycle();
            }

            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int childAdapterPosition = parent.getChildAdapterPosition(view);
                outRect.set(0,5*childAdapterPosition,0,5*childAdapterPosition);
            }
        });
        adapter.setOnChildClickListener(this);
    }

    @Override
    public void onClick(RecyclerView recyclerView, View view, int position, String data) {
//        Toast.makeText(MainActivity.this, position+":"+data, Toast.LENGTH_SHORT).show();
//        adapter.remove(position);
//        adapter.add(position,"asdasf");
//        adapter.change(position,"被改变了");
        arrayList.clear();
        arrayList.addAll(mPresenter.update());
        adapter.notifyDataSetChanged();
    }

    private static final String TAG = "MainActivity";
    public void Recyclerviewtest01(View view) {
        mPresenter.getRequest();
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("https://www.baidu.com/")
//                .addConverterFactory(
//                        new Converter.Factory() {
//                            @Override
//                            public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
//                                return new Converter<ResponseBody, String>() {
//                                    @Override
//                                    public String convert(ResponseBody value) throws IOException {
//                                        return value.string();
//                                    }
//                                };
//                            }
//                        }
//                ).build();
//        Service service = retrofit.create(Service.class);
//        service.getBaidu().enqueue(new Callback<String>() {
//            @Override
//            public void onResponse(Call<String> call, Response<String> response) {
//
//            }
//
//            @Override
//            public void onFailure(Call<String> call, Throwable t) {
//
//            }
//        });
    }

    @Override
    public void loginSuccess(String msg) {
        textView.setText(msg);
    }

    @Override
    public void loginError(String msg) {
        textView.setText(msg);
    }
}
